/**
 * 新增修改完成调用 modalFormOk 方法 编辑弹框组件ref定义为modalForm
 * data中url定义 list为查询列表  delete为删除单条记录  deleteBatch为批量删除
 */
import { postAction } from '@/api/manage'

export const listMixin = {
  data() {
    return {
      queryParam: {}, // 查询条件-请不要在queryParam中声明非字符串值的属性
      dataSource: [], // 数据源
      ipagination: { // 分页参数
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      selectedRowList: [] // table选中列表
    }
  },
  created() {
    // 列表页data定义 disableMixinCreated: true, 初始不调用loadData()
    if (!this.disableMixinCreated) {
      this.loadData()
    }
  },
  methods: {
    // 查询
    searchQuery() {
      this.loadData(1)
      this.selectedRowList = []
    },
    // 重置
    searchReset() {
      // 深度重置
      const object = this.queryParam
      for (const key in object) {
        const type = Object.prototype.toString.call(object[key])
        if (type === '[object Array]') {
          object[key] = []
        } else if (type === '[object Object]') {
          object[key] = {}
        }
      }
      this.queryParam = {}
      this.loadData(1)
    },
    // 获取查询条件
    getQueryParams() {
      const newParam = this.addQueryParams()
      const param = { ...this.queryParam, ...newParam }
      param.pageNum = this.ipagination.pageNum
      param.pageSize = this.ipagination.pageSize
      return param
    },
    // 添加查询参数 默认加空对象
    addQueryParams() {
      return {}
    },
    // 加载数据列表
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.pageNum = 1
      }
      const params = this.getQueryParams() // 查询条件
      this.$store.dispatch('app/setLoading', true)
      postAction(this.url.list, params).then((res) => {
        console.log('res', res)
        if (res.status === 0) {
          const { list, total } = res.data
          this.dataSource = list
          console.log(' this.dataSource ', this.dataSource)
          if (total) {
            this.ipagination.total = total
          } else {
            this.ipagination.total = 0
          }
        } else {
          this.$message.warning(res.message || '操作失败！')
        }
      }).finally(() => {
        this.$store.dispatch('app/setLoading', false)
      })
    },
    handleView(title, rowData) {
      this.$refs.modalFormView.show(rowData)
      this.$refs.modalFormView.title = title
    },
    handleAdd(title) {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = title
      this.$refs.modalForm.disableSubmit = false
    },
    handleEdit(title, rowData) {
      this.$refs.modalForm.edit(rowData)
      this.$refs.modalForm.title = title
      this.$refs.modalForm.disableSubmit = true
    },
    handleDelete(id) {
      console.log('id', id)
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      const that = this
      this.$store.dispatch('app/setLoading', true)
      postAction(that.url.delete, { id }).then((res) => {
        if (res.code === 1) {
          that.reCalculatePage(1)
          that.$message.success(res.message || '操作成功！')
          that.loadData()
          that.onClearSelected()
        } else {
          that.$message.warning(res.message || '操作失败！')
        }
      }).finally(() => {
        this.$store.dispatch('app/setLoading', false)
      })
    },
    // 重新计算分页
    reCalculatePage(count) {
      // 总数量-count
      const total = this.ipagination.total - count
      // 获取删除后的分页数
      const currentIndex = Math.ceil(total / this.ipagination.pageSize)
      // 删除后的分页数<所在当前页
      if (currentIndex < this.ipagination.pageNum) {
        this.ipagination.pageNum = currentIndex
      }
      if (this.ipagination.pageNum < 1) {
        this.ipagination.pageNum = 1
      }
    },
    // 新增/修改 成功时
    modalFormOk() {
      this.loadData()
      this.onClearSelected() // 清空列表选中
    },
    // 清空列表选中
    onClearSelected() {
      this.$refs.table?.clearSelection()
    },
    // 表格多选Change
    onSelectChange(val) {
      this.selectedRowList = val.map(v => v[this.guidFieldName])
    },
    // 批量删除
    batchDelete() {
      if (!this.url.deleteBatch) {
        this.$message.error('请设置url.deleteBatch属性!')
        return
      }
      if (this.selectedRowList.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        const that = this
        this.$confirm('是否删除选中数据?', '确认删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log('要删除的ID', this.selectedRowList)
          that.$store.dispatch('app/setLoading', true)
          postAction(that.url.deleteBatch, { idList: this.selectedRowList }).then((res) => {
            if (res.code === 1) {
              // 重新计算分页问题
              that.reCalculatePage(that.selectedRowList.length)
              that.$message.success(res.message || '操作失败！')
              that.loadData()
              that.onClearSelected()
            } else {
              that.$message.warning(res.message || '操作失败！')
            }
          }).finally(() => {
            this.$store.dispatch('app/setLoading', false)
          })
        })
      }
    }
  }
}
